package parser

import (
	"bytes"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"my-crawler/concurrence/engine"
	"my-crawler/concurrence/fetcher"
)

//使用 goquery 获取网页dom元素
func GetCityListParser(content []byte, ReqData engine.ReqData) engine.ParserResult {
	reader := bytes.NewReader(content)
	doc, err := goquery.NewDocumentFromReader(reader)
	if err != nil {
		fmt.Errorf("goquery: Load the HTML document failed! %v", err)
	}
	res := engine.ParserResult{}

	doc.Find("#cityList dd a").Each(func(i int, s *goquery.Selection) {
		// For each item found, get the title
		//href, _ := s.Attr("href")

		res.Request = append(res.Request, engine.CrawlerRequest{
			HttpFunc: &fetcher.GetReq{},
			Parser:   GetCityListParser,
		})
	})
	return res
}
